package com.one.group.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.InventoryFlowAddDto;
import com.one.group.model.dto.InventoryFlowQueryDto;
import com.one.group.model.dto.InventoryFlowUpdateDto;
import com.one.group.model.vo.InventoryFlowListVo;
import com.one.group.result.Result;
import com.one.group.service.InventoryFlowService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("inventoryFlow")
@Tag(name = "库存流水管理接口")
public class InventoryFlowController {
    @Autowired
    InventoryFlowService inventoryFlowService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("list")
    @Operation(summary = "查询库存流水列表")
    public Result list(@RequestBody InventoryFlowQueryDto inventoryFlowQueryDto) {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        inventoryFlowQueryDto.setTenantId(tenantId);
        PageHelper.startPage(inventoryFlowQueryDto.getPage(), inventoryFlowQueryDto.getLimit());
        List<InventoryFlowListVo> inventoryFlowListVos = inventoryFlowService.findInventoryFlowByQueryDto(inventoryFlowQueryDto);
        PageInfo<InventoryFlowListVo> pageInfo = new PageInfo<>(inventoryFlowListVos);
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询库存流水信息")
    public Result find(@RequestParam Long id) {
        return ResultUtil.success(inventoryFlowService.findInventoryFlowById(id));
    }

    @GetMapping("findAll")
    @Operation(summary = "查询所有库存流水信息")
    public Result findAll() {
        return ResultUtil.success(inventoryFlowService.list());
    }

    @GetMapping("/delete")
    @Operation(summary = "删除库存流水信息")
    public Result delete(@RequestParam Long id) {
        if (inventoryFlowService.removeInventoryFlowById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @PostMapping("/add")
    @Operation(summary = "添加库存流水信息")
    public Result add(@RequestBody InventoryFlowAddDto inventoryFlowAddDto) {

        if (inventoryFlowService.insertInventoryFlow(inventoryFlowAddDto)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @PostMapping("/update")
    @Operation(summary = "修改库存流水信息")
    public Result update(@RequestBody InventoryFlowUpdateDto inventoryFlowUpdateDto) {

        if (inventoryFlowService.updateInventoryFlow(inventoryFlowUpdateDto)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除库存流水信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto) {
        if (inventoryFlowService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
